package Test.Test;

import javax.print.ServiceUIFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class File_IO {
    public static void main(String[] args){
//        createFile1();
//        createFile2();
//        createFile3();
//        createDir();
//        createDirs();
//        delete1();
//        delete2();
//        File file = new File("d:\\test.txt");
//        System.out.println(file.exists());
//
//        if(!file.exists()){
//            System.out.println(file.createNewFile());
//        }
//
//        System.out.println(file.isDirectory());
//
//        System.out.println(file.isFile());
//
//        if(file.exists()){
//            System.out.println(file.delete());
//        }

//        File file = new File("doc\\a.txt"); //相对路径
//        File files = new File("doc");
//        files.mkdir();
//        file.createNewFile();
//        String relativePath = file.getPath(); //获取相对路径
//        System.out.println(relativePath);
//        String absolutePath = file.getAbsolutePath();
//        System.out.println(absolutePath);
//        String filename = file.getName();
//        System.out.println(filename);
//        Long filesize = file.length();
//        System.out.println(filesize);
//        File file = new File("d:\\");
//        File[] files = file.listFiles();
//        for(File li : files){
//            System.out.println(li.getName());
//        }

        // ---------字节流

//
//        fun1();
//        fun2();
//        fun3();
//        fun4();
//        fun5();
//        fun6();
//        FileCopy_demo2();

//        try {
//            InputStream fis = new FileInputStream("d:\\test1.txt");
//            InputStream bufferIn = new BufferedInputStream(fis);
//            OutputStream fos = new FileOutputStream("d:\\test2.txt");
//            OutputStream bufferOut = new BufferedOutputStream(fos);
//            byte[] b = new byte[fis.available()];
//            bufferIn.read(b);
//            bufferOut.write(b);
////            int len;
////            while((len = bufferIn.read(b)) != -1){
////                System.out.println("读取：" + len);
////                bufferOut.write(b);
////                b = new byte[4];
////            }
//            bufferOut.flush();
//        }catch (IOException e){
//            e.printStackTrace();
//        }

//        reader_Demo3();
//        writer_Demo1();
//        try{
//            Reader reader = new FileReader("doc\\a.txt");
//            BufferedReader br = new BufferedReader(reader);
//            String line;
//            while((line = br.readLine()) != null ){
//                System.out.println(line);
//            }
//        }catch (IOException e){
//            e.printStackTrace();
//        }
//
//        try{
//            Writer writer = new FileWriter("doc\\a.txt", true);
//            BufferedWriter bw = new BufferedWriter(writer);
//            bw.write("春眠不觉晓\r\n");
//            bw.write("处处闻啼鸟");
//            bw.newLine();
//            bw.write("adidas");
//            bw.flush();
//
//            Reader reader = new FileReader("doc\\a.txt");
//            BufferedReader br = new BufferedReader(reader);
//            String line;
//            while((line = br.readLine()) != null){
//                System.out.println(line);
//            }
//        }catch (IOException e){
//            e.printStackTrace();
//            System.out.println(e.getMessage());
//        }

//        inputstreamReaderDemo1();
//        inputStreamReaderDemo2();

//        try{
//            OutputStream fos = new FileOutputStream("doc\\a.txt", true);
//            Writer writer = new OutputStreamWriter(fos);
//            BufferedWriter bw = new BufferedWriter(writer);
//            bw.write("asdwqdss");
//            bw.newLine();
//            bw.flush();
//        }catch (IOException e){
//            e.printStackTrace();
//            System.out.println(e.getMessage());
//        }

//        try{
//            OutputStream fos = new FileOutputStream("doc\\a.txt", true);;
//            PrintStream ps = new PrintStream(fos);
//            ps.println("你好");
//            ps.println(18);
//            ps.println(true);
//            System.out.println("");
//        }catch (IOException e){
//            e.printStackTrace();
//        }
    }


    public static void createFile1(){
        File file = new File("d:\\test1.txt");
        try{
            System.out.println(file.createNewFile());
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void createFile2(){
        File file = new File("d:\\aa", "test2.txt");
        try{
            System.out.println(file.createNewFile());
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public static void createFile3(){
        File file = new File("d:\\aa\\bb");
        File file2 = new File(file, "test3.txt");
        try{
            System.out.println(file2.createNewFile());
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public static void createDir(){
        File file = new File("d:\\cc");
        file.mkdir();
    }

    public static void createDirs(){
        File file = new File("d:\\asc\\avv\\asd");
        file.mkdirs();
    }

    public static void delete1(){
        File file = new File("d:\\test.txt");
        file.delete();
    }

    public static void delete2(){
        File files = new File("d:\\aa");
        File file = new File("d:\\aa\\test2.txt");
        try{
            System.out.println(file.createNewFile());
        }catch(IOException e){
            e.printStackTrace();
        }
        System.out.println(files.delete());
        System.out.println("先删除文件再删除文件夹");
        System.out.println(file.delete());
        System.out.println(files.delete());
    }

    public static void fun1() {//读取文件所有字节
        try{
            InputStream fis = new FileInputStream("d:\\test.txt");
            int data;
            while ((data = fis.read()) != -1){
                System.out.print((char) data);
            }
        } catch (IOException e){
//            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void fun2(){//利用缓冲区一次性读取多个字节
        try{
            InputStream fis = new FileInputStream("d:\\test1.txt");
            byte[] b = new byte[6];
            int data;
            while((data = fis.read(b)) != -1){
                System.out.println(new String(b));
                b = new byte[6];
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void fun3(){//一次性读取所有字节
        try{
            InputStream fis = new FileInputStream("d:\\test1.txt");
            byte[] b = new byte[fis.available()];
            int data = fis.read(b);
            System.out.println(new String(b));
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void fun4(){
        OutputStream out = null;
        try{
            out = new FileOutputStream("d:/test1.txt", true );
            out.write(72);
            out.write(101);
            out.write(108);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void fun5(){
        OutputStream fos = null;
        try{
            fos = new FileOutputStream("d:\\test1.txt", true);
            String word = "asdaw1234142";
            byte[] words = word.getBytes();
            for(int i = 0; i < word.length(); i ++ ){
                fos.write(words[i]);
            }
        }catch (IOException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }finally {
            if(fos != null){
                try{
                    fos.close();
                }catch (IOException e){
                    e.printStackTrace();
                    System.out.println(e.getMessage());
                }
            }
        }
    }

    public static void fun6(){
        OutputStream fos = null;
        try{
            fos = new FileOutputStream("d:\\test1.txt", true);
            fos.write("爱思考DJ舞曲".getBytes(StandardCharsets.UTF_8));
        }catch (IOException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        finally {
            try{
                if(fos != null){
                    fos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
        }
    }

    public static void FileCopy_demo1(){
        try{
            InputStream fis = new FileInputStream("d:\\test1.txt");
            File file = new File("d:\\test2.txt");
            file.createNewFile();
            OutputStream fos = new FileOutputStream(file);
            int data;
            while((data = fis.read()) != -1){
                fos.write(data);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void FileCopy_demo2(){
        try{
            InputStream fis = new FileInputStream("d:\\test1.txt");
            OutputStream fos = new FileOutputStream("d:\\test2.txt");
            byte[] b = new byte[fis.available()];
            fis.read(b);
            fos.write(b);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void reader_Demo1(){
        try{
            Reader reader = new FileReader("doc\\a.txt");
            int data;
            while((data = reader.read()) != -1){
                System.out.print((char) data);
            }
        }catch (IOException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void reader_Demo2(){
        try{
            Reader reader = new FileReader("doc\\a.txt");
            char[] arr = new char[100];
            int num = reader.read(arr);
            System.out.println(new String(arr));
        }catch (IOException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void reader_Demo3(){
        try{
            Reader reader = new FileReader("doc\\a.txt");
            char[] arr = new char[6];
            int num;
            while((num = reader.read(arr)) != -1){
                System.out.println(new String(arr));
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void writer_Demo1(){
        try{
            Writer writer = new FileWriter("doc\\a.txt", true);
            writer.write("窗前明月光\r\n");
            writer.write("疑是地上霜\n");
            writer.flush();
        }catch (IOException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

    public static void inputstreamReaderDemo1(){
        try{
            InputStream fis = new FileInputStream("doc\\a.txt");
            Reader reader = new InputStreamReader(fis, "UTF-8");

            int data;
            while((data = reader.read()) != -1){
                System.out.print((char) data);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void inputStreamReaderDemo2(){
        try{
            InputStream fis = new FileInputStream("doc\\a.txt");
            Reader reader = new InputStreamReader(fis, StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(reader);
            String line;
            while((line = br.readLine()) != null){
                System.out.println(line);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
